namespace DotNetNuke.Security.Permissions
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Data;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Security;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class ModulePermissionController
    {
        public int AddModulePermission(ModulePermissionInfo objModulePermission)
        {
            return DataProvider.Instance().AddModulePermission(objModulePermission.ModuleID, objModulePermission.PermissionID, objModulePermission.RoleID, objModulePermission.AllowAccess);
        }

        public void DeleteModulePermission(int modulePermissionID)
        {
            DataProvider.Instance().DeleteModulePermission(modulePermissionID);
        }

        public void DeleteModulePermissionsByModuleID(int ModuleID)
        {
            DataProvider.Instance().DeleteModulePermissionsByModuleID(ModuleID);
        }

        public ModulePermissionInfo GetModulePermission(int modulePermissionID)
        {
            return (ModulePermissionInfo) CBO.FillObject(DataProvider.Instance().GetModulePermission(modulePermissionID), typeof(ModulePermissionInfo));
        }

        public string GetModulePermissionsByModuleID(ModuleInfo objModule, string PermissionKey)
        {
            IEnumerator refObjectHelperL0=null;
            ModulePermissionCollection objModPerms = objModule.ModulePermissions;
            string strRoles = ";";
            try
            {
                refObjectHelperL0 = objModPerms.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    ModulePermissionInfo objModulePermission = (ModulePermissionInfo) refObjectHelperL0.Current;
                    if (((objModulePermission.AllowAccess && (objModulePermission.PermissionKey == PermissionKey)) ? 1 : 0) != 0)
                    {
                        strRoles = strRoles + DotNetNuke.Common.Globals.GetRoleName(objModulePermission.RoleID) + ";";
                    }
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return strRoles;
        }

        public ArrayList GetModulePermissionsByPortal(int PortalID)
        {
            return CBO.FillCollection(DataProvider.Instance().GetModulePermissionsByPortal(PortalID), typeof(ModulePermissionInfo));
        }

        public ModulePermissionCollection GetModulePermissionsCollectionByModuleID(int moduleID)
        {
            IList objModulePermissionCollection = new ModulePermissionCollection();
            CBO.FillCollection(DataProvider.Instance().GetModulePermissionsByModuleID(moduleID, -1), typeof(ModulePermissionInfo), ref objModulePermissionCollection);
            return (ModulePermissionCollection) objModulePermissionCollection;
        }

        public string GetRoleNamesFromRoleIDs(string Roles)
        {
            string strRoles = "";
            if (Roles.IndexOf(";") > 0)
            {
                string[] arrRoles = Strings.Split(Roles, ";", -1, CompareMethod.Binary);
                int refIntHelperL0 = arrRoles.Length - 1;
                for (int i = 0; i <= refIntHelperL0; i++)
                {
                    if (Versioned.IsNumeric(arrRoles[i]))
                    {
                        strRoles = strRoles + DotNetNuke.Common.Globals.GetRoleName(Convert.ToInt32(arrRoles[i])) + ";";
                    }
                }
            }
            else if (Roles.Trim().Length > 0)
            {
                strRoles = DotNetNuke.Common.Globals.GetRoleName(Convert.ToInt32(Roles.Trim()));
            }
            if (!strRoles.StartsWith(";"))
            {
                strRoles = strRoles + ";";
            }
            return strRoles;
        }

        public static bool HasModulePermission(ModulePermissionCollection objModulePermissions, string PermissionKey)
        {
            ModulePermissionCollection m = objModulePermissions;
            int refIntHelperL0 = m.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                ModulePermissionInfo mp = m[i];
                if ((((mp.PermissionKey == PermissionKey) && PortalSecurity.IsInRoles(mp.RoleName)) ? 1 : 0) != 0)
                {
                    return true;
                }
            }
            return false;
        }

        public void UpdateModulePermission(ModulePermissionInfo objModulePermission)
        {
            DataProvider.Instance().UpdateModulePermission(objModulePermission.ModulePermissionID, objModulePermission.ModuleID, objModulePermission.PermissionID, objModulePermission.RoleID, objModulePermission.AllowAccess);
        }
    }
}

